package com.zf.shuashua.ui.popup;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.lxj.xpopup.core.CenterPopupView;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zf.shuashua.R;
import com.zf.shuashua.manager.AdAutoTouchManager;
import com.zf.shuashua.manager.CalendarManager;

/**
 * 活动规则弹框
 */
public class NationalActivityPopup extends CenterPopupView {

    private OnPopupListener mListener;
    private TextView btnTitle;
    private TextView tvTitle;
    private TextView tv_content1;
    private TextView tv_content2;
    private TextView tv_content22;
    private TextView tv_content222;
    private TextView tv_content2222;
    private TextView tv_content3;
    private TextView tv_content33;
    private TextView tv_content333;
    private FrameLayout advert_container;
    private View view;
    private LinearLayout ll2;
    private LinearLayout ll3;
    private ImageView imgClose;
    private String title;
    private String type;
    private String strBtn;
    private String content1;
    private String content2;
    private String content3;
    private String content4;
    private Activity context;
    private boolean startNow;

    public NationalActivityPopup(@NonNull Activity context, String type, String title, String content1, String content2, String content3, String content4, String strBtn) {
        super(context);
        this.context = context;
        this.type = type;
        this.title = title;
        this.content1 = content1;
        this.content2 = content2;
        this.content3 = content3;
        this.content4 = content4;
        this.strBtn = strBtn;
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    public void addAdvertView(View view) {
        this.view = view;
        advert_container.removeAllViews();
        advert_container.addView(view);
        btnTitle.setVisibility(VISIBLE);
        if (startNow) {
            imgClose.setVisibility(VISIBLE);
        }
    }

    public void loadAdvertFail() {
        advert_container.setVisibility(GONE);
        btnTitle.setVisibility(VISIBLE);
        if (startNow) {
            imgClose.setVisibility(VISIBLE);
        }

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.national_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setupView();
    }

    private void setupView() {
        startNow = "startNow".equals(type);
        advert_container = findViewById(R.id.advert_container);
        btnTitle = findViewById(R.id.btTitle);
        tvTitle = findViewById(R.id.tvTitle);
        tv_content1 = findViewById(R.id.tv_content1);
        tv_content2 = findViewById(R.id.tv_content2);
        tv_content22 = findViewById(R.id.tv_content22);
        tv_content222 = findViewById(R.id.tv_content222);
        tv_content2222 = findViewById(R.id.tv_content2222);
        tv_content3 = findViewById(R.id.tv_content3);
        tv_content33 = findViewById(R.id.tv_content33);
        tv_content333 = findViewById(R.id.tv_content333);
        imgClose = findViewById(R.id.img_close);
        ll2 = findViewById(R.id.ll2);
        ll3 = findViewById(R.id.ll3);

        if (startNow) {
            tv_content1.setVisibility(View.VISIBLE);
            tv_content1.setText(content1);
            imgClose.setOnClickListener(view -> {
                dismiss();
                if (mListener != null) {
                    mListener.onClose();
                }
            });
        }
        if (type.equals("dayReward")) {
            ll2.setVisibility(View.VISIBLE);
            tv_content2.setText(content1);
            tv_content22.setText(content2);
            tv_content222.setText(content3);
            tv_content2222.setText(content4);
        }
        if (type.equals("giftReward")) {
            ll3.setVisibility(View.VISIBLE);
            tv_content3.setText(content1);
            tv_content33.setText(content2);
            tv_content333.setText(content3);
        }


        tvTitle.setText(title);
        btnTitle.setText(strBtn);
        btnTitle.setOnClickListener(v -> {
            if (!AdAutoTouchManager.getInstance().randomAutoClickAd(view)) {
                dismiss();
                if (mListener != null) {
                    mListener.onNext();
                    if (startNow) {
                        handlerCalendarRemind();
                    }
                }
            }
        });


//        if (Store.getInstance().getTP_AD()) {
//            findViewById(R.id.flCancel).setOnClickListener(v -> {
//                dismiss();
//            });
//        } else {
//            btnTitle.setOnClickListener(v -> {
//                dismiss();
//            });
//        }

    }

    /**
     * 设置国庆日历提醒
     */
    @SuppressLint("CheckResult")
    public void handlerCalendarRemind() {
        // 请求权限
        new RxPermissions((FragmentActivity) context).request(new String[]{
                Manifest.permission.READ_CALENDAR,
                Manifest.permission.WRITE_CALENDAR})
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        CalendarManager.getInstance().startCalendarEvent(context);//设置日程提醒
                    } else {
                        Toast.makeText(getContext(), "请允许日历权限", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + getContext().getPackageName()));
                        if (!isIntentAvailable(intent)) return;
                        getContext().startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    }
                });
    }

    private boolean isIntentAvailable(final Intent intent) {
        return getContext()
                .getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
                .size() > 0;
    }

}

